//
//  DYGJGVC.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/6/21.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "DYGJGVC.h"
#import "DYGJGCell.h"
#import <Masonry/Masonry.h>
#import "DYGJGModel.h"

@interface DYGJGVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) UIImageView * imgView;
@property (nonatomic,assign) NSInteger pageNum;
@property (nonatomic,assign) NSInteger pageSize;
@property (nonatomic,strong) NSMutableArray * models;
@end

@implementation DYGJGVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self collectionView];
    self.title = @"国家馆";
    self.pageNum = 1;
    self.pageSize = 10;
    self.models = [NSMutableArray array];
    [self loadData];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self hiddenShadowImage:NO];
    [self navigationSetup];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
}

- (void)navigationSetup {
    [self hideNavigationController];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DYGJGCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    [cell loadData:self.models[indexPath.row]];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.models.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)loadData{
    __weak typeof(self) weakSelf = self;
    [MYAFNetworking POST:@"api2/getCountryPagerList" parameters:@{@"pageNumber":@(self.pageNum),@"pageSize":@10,@"keyword":@""} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        NSString * code = [responseObject safeObjectForKey:@"code"];
        if ([code isEqualToString:@"200"]) {
            id data = [responseObject safeObjectForKey:@"data"];
            NSArray * arr = [data safeObjectForKey:@"list"];
            bool isFisrt = [[data safeObjectForKey:@"firstPage"] boolValue];
            bool isLast = [[data safeObjectForKey:@"lastPage"] boolValue];
            if (isFisrt) {
                weakSelf.models = [arr mutableCopy];
            }else{
                [weakSelf.models addObjectsFromArray:arr];
            }
            if (isLast) {
                [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
            }
            [weakSelf.collectionView.mj_header endRefreshing];
            [weakSelf.collectionView.mj_footer endRefreshing];
            [weakSelf.collectionView reloadData];
        }else{
            [MBProgressHUD showError:weakSelf.view title:[responseObject safeStringForKey:@"message"] code:code animated:YES];
            weakSelf.pageNum--;
            if (weakSelf.pageNum < 1) {
                weakSelf.pageNum = 1;
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        weakSelf.pageNum--;
        if (weakSelf.pageNum < 1) {
            weakSelf.pageNum = 1;
        }
    }];
}


- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(ScreenWidth * .25, 110);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 10;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        [_collectionView registerClass:[DYGJGCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.view addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.imgView.mas_bottom);
        }];
        __weak typeof(self) weakSelf = self;
        _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.pageNum = 1;
        }];
        _collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            weakSelf.pageNum++;
        }];
    }
    return _collectionView;
}
- (UIImageView *)imgView{
    if (!_imgView) {
        _imgView  = [[UIImageView alloc] init];
        [self.view addSubview:_imgView];
        [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.view);
            make.height.equalTo(@180);
        }];
    }
    return _imgView;
}

@end
